Preface
Summarize what I learned from the data structure class and some of its implementation in different language.
Category
- Array
- LinkedList
- Stack
- Queue
- Heap
- Map
- HashMap/Dictionary
- LinkedHashMap/OrderedDict
- Tree
- Binary Search Tree
- B tree
- B-tree
- B+tree
- B* tree
- Red-Black Tree
- Graph
- Quick Find
- Quick Union
- Weighted Quick Union
- Path Compression
- Weighted + Path
Array
Structure
Member Function Time Complexity
Action | Worst Time Complexity |
---|---|
Add | O(1) |
Access By Index | O(1) |
Search | O(N) |
Insert | O(N) |
Resize | O(N) |
Delete | O(N) |
Implementation in different language
C++
Array—Fixed Size In Stack
Example:1
2
3
4
5
6
7
8
9
10
11
12
13
14
using namespace std;
int main(){
int arr[]={10,20,40,60};
int *p=arr;
cout<<arr<<endl;
cout<<arr+1<<endl;
cout<<*p<<endl;
cout<<*(p+2)<<endl;
cout<<p[3]<<endl;
cout<<&p<<endl;
return 0;
}Dynamic Array–Can Change Size During Runtime
1 | int* a = NULL; // Pointer to int, initialize to nothing. |
Java
- Plain Array
Much Similar As what is done in C++ - ArrayList(Defined in JDK library)
Def: An Object Class that implements List Interface
Note that ArrayList used static function of Arrays to do the resize
1 | public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) |
- The Java Virtual Machine used reflection mechanism to initialize a new Array object with the new size during runtime
- Copy the old element to the new Array
Python
In python, one can use list to serve as an array
Linked List
Structure
Member Function Time Complexity
Action | Worst Time Complexity |
---|---|
Add | O(1) |
Access By Index | O(N) |
Search | O(N) |
Insert | O(N) |
Resize | O(1) |
Delete | O(N) |
Implementation in different language
C++
Example:
1 |
|
Java
Predefined LinkedList that implements List interface
Python
Def Node
1 | class Node(object): |
Def linkedList
1 | class LinkedList(object): |
Stack
FILO: First In Last Out
Structure
Member Function Time Complexity
Action | Worst Time Complexity |
---|---|
Push | O(1) |
Pop | O(1) |
Implementation in Different Language
C++
1 |
|
Java
Can use ArrayList to serve as Stack
Python
Can use list
Queue
FIFO:First In Last Out
Structure
Heap
- MinHeap
- MaxHeap
Can be implemented using Array
Time Complexity
Action | Worst Time Complexity | Step |
---|---|---|
insert | O(logN) | NA |
delete min | O(logN) | Switch min with the last entry and heapify |
Sort | O(NlogN) | build a min heap, delete min iteratively |
Map
Structure
Hashing
- Probing
- Linear Probing: When Collision happens, always find the nex spot
- Quadratic Probing: When Collision happens, always find the next i^2 spots
- Chained Hashing
- When collision happens, Link the same hash element
Implementation
- Python
- Dict
- OrderedDict
- Java
- HashMap
- LinkedHashMap
Tree
- Binary Tree
- Full Binary Tree
- Complete Binary Tree
- Consider the Skewed Case
Action | Worst Time Complexity |
---|---|
insert | O(N) |
delete | O(N) |
search | O(N) |
- B tree(Self Balanced)
Action | Worst Time Complexity |
---|---|
insert | O(logN) |
delete | O(logN) |
search | O(logN) |
Difference Between different B tree
- B-tree: Self Balanced Search Tree
- B+tree: Only the leaf Node store the data, Non-leaf node only contains the key as road map
- B* tree: Contains the pointer to brother node
- Red-Black Tree
- Root of the tree is always black
- No adjacent red node
- Every path from a node to any of its descendent null node has the same number of black node
Graph
With M union and find Operation on N graph Node objects
Time Complexity
type of union-find | Worst Time Complexity |
---|---|
Quick-find | MN |
Quick-union | MN |
weighted Union | N+MlogN |
Path Compression | N+MlogN |
weighted+Path | (M+N)log* N |